package com.core.service.impl;

import com.core.dto.UserInfoDto;
import com.core.dto.UserDetailsDto;
import com.core.service.UsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * @author : HUANG
 * @create : 2025/6/1 20:28
 * @function :
 */
@Slf4j
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UsersService usersService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        UserInfoDto userInfo = usersService.selectInfoByUser(s);
        log.info("查询用户：{}，密码：{}，权限：{}", userInfo.getUsername(), userInfo.getPassword(), Arrays.toString(userInfo.getRolesPermissions()));
        if (userInfo == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        return new UserDetailsDto(userInfo);
    }
}
